/* QChart.qml ---
 *
 * Author: Julien Wintz
 * Created: Thu Feb 13 20:59:40 2014 (+0100)
 * Version:
 * Last-Updated: jeu. mars  6 12:55:14 2014 (+0100)
 *           By: Julien Wintz
 *     Update #: 69
 */

/* Change Log:
 *
 */

import QtQuick 2.0
import "QChart.js" as Charts
import "QChartGallery.js" as ChartsData
Canvas {

    id: canvas;

    // ///////////////////////////////////////////////////////////////

    property   var chart;
    property   var chartData;
    property   int chartType: 0;
    property  bool chartAnimated: true;
    property alias chartAnimationEasing: chartAnimator.easing.type;
    property alias chartAnimationDuration: chartAnimator.duration;
    property   int chartAnimationProgress: 0;

    // //////////////////////////////////////////////////////////////
    property var xMinDiffPoint : 0;
    property var yMinDiffPoint : 0;
    property var indexMinDiffPoint : 0;

    // /////////////////////////////////////////////////////////////////
    // Callbacks
    // /////////////////////////////////////////////////////////////////

    onPaint: {
        id:paint
        chart = new Charts.Chart(canvas, canvas.getContext("2d")).Line(chartData);

        chart.init();

        if (chartAnimated)
            chartAnimator.start();
        else
            chartAnimationProgress = 100;
        chart.draw(chartAnimationProgress/100);
    }

    onHeightChanged: {
        requestPaint();
    }

    onWidthChanged: {
        requestPaint();
    }

    onChartAnimationProgressChanged: {
        requestPaint();
    }
    onChartDataChanged: {
        requestPaint();
    }

    // /////////////////////////////////////////////////////////////////
    // Functions
    // /////////////////////////////////////////////////////////////////

    function repaint() {
        //   chartAnimationProgress = 0;
        chartAnimator.start();
    }

    // /////////////////////////////////////////////////////////////////
    // Internals
    // /////////////////////////////////////////////////////////////////

    PropertyAnimation {
        id: chartAnimator;
        target: canvas;
        property: "chartAnimationProgress";
        to: 100;
        duration: 500;
        easing.type: Easing.InOutElastic;
    }

    MouseArea{
        anchors.fill: chart_line
        onClicked: {

            canvas.chart.getClickPoint(mouseX,mouseY);
            //console.log(canvas.chart.getPointX()+" "+canvas.chart.getPointY());
            canvas.xMinDiffPoint=canvas.chart.getPointX();
            canvas.yMinDiffPoint=canvas.chart.getPointY();
            canvas.indexMinDiffPoint=canvas.chart.getPointIndex();

        }
        onDoubleClicked: {
            var ChartLineData = {
                labels: [1,2,3],
                datasets: [{
                        fillColor: "rgba(220,220,220,0.5)",
                        strokeColor: "rgba(220,220,220,1)",
                        pointColor: "rgba(220,220,220,1)",
                        pointStrokeColor: "#ffffff",
                        data: [1,2,3]
                    }]
            }


            // ChartLineData.labels.push(123);
            canvas.chartData=ChartLineData;
            //   chart_line.repaint();
        }
    }

}
